package com.nimitz.module.drip.service.impl;

import com.nimitz.common.constant.OrderConstants;
import com.nimitz.common.utils.bean.BeanUtils;
import com.nimitz.module.drip.domain.DriBusiness;
import com.nimitz.module.drip.domain.DriCash;
import com.nimitz.module.drip.mapper.DriCashMapper;
import com.nimitz.module.drip.service.IDriBusinessService;
import com.nimitz.module.drip.service.IDriCashService;
import com.nimitz.module.drip.service.IDriIncomeService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 现金流水 业务层实现
 */
@Service
public class DriCashServiceImpl implements IDriCashService {

    private static final Logger logger = LoggerFactory.getLogger(DriCashServiceImpl.class);

    @Resource
    private DriCashMapper cashMapper;

    @Resource
    private IDriBusinessService businessService;

    @Resource
    private IDriIncomeService incomeService;

    /**
     * 生成现金流水
     *
     * @param brandName 品牌名称
     * @param orderDate 订单日期
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int generateCash(String brandName, String orderDate) {
        incomeService.testIncome();
        // 线程延时队列-开始
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
        executorService.schedule(() -> {
            try {
                // 日营业额中查找现金流水
                List<DriBusiness> businessList = businessService.selectBusinessListByOrderTypeAndOrderDate(brandName, OrderConstants.ORDER_TYPE_PURCHASE, orderDate);
                if (CollectionUtils.isNotEmpty(businessList)) {
                    // 批量插入现金流水
                    this.insertCash(brandName, businessList);
                    // 修改日营业额现金流水状态
                    this.updateBusinessByCashStatus(brandName, businessList);
                }
            } catch (Exception e) {
                logger.info("生成现金流水" + e.getMessage());
                e.printStackTrace();
            } finally {

            }
        }, 0, TimeUnit.SECONDS);
        // 线程延时队列-结束
        return 1;
    }

    /**
     * 新增现金流水
     *
     * @param brandName    品牌名称
     * @param businessList 日营业额集合
     * @return 结果
     */
    @Override
    public int insertCash(String brandName, List<DriBusiness> businessList) {
        List<DriCash> cashList = new ArrayList<>();
        for (DriBusiness business : businessList) {
            DriCash cash = new DriCash();
            BeanUtils.copyProperties(business, cash);
            cashList.add(cash);
        }
        return cashMapper.insertCash(brandName, cashList);
    }

    /**
     * 修改日营业额现金流水状态
     *
     * @param brandName    品牌名称
     * @param businessList 日营业额集合
     * @return 结果
     */
    public int updateBusinessByCashStatus(String brandName, List<DriBusiness> businessList) {
        List<Integer> businessIds = businessList.stream().map(DriBusiness::getBusinessId).collect(Collectors.toList());
        return businessService.updateBusinessByCashStatus(brandName, businessIds);
    }
}
